package com.zglc.ms.shop.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.zglc.ms.shop.dao.ProductDao;
import com.zglc.ms.shop.dao.ProductDao.ProductSimpleInfo;
import com.zglc.ms.shop.dao.ProductLimitDao;
import com.zglc.ms.shop.dao.ProductPriceDao;
import com.zglc.ms.shop.model.ShopProduct;
import com.zglc.ms.shop.model.ShopProductLimit;
import com.zglc.ms.shop.util.CommonUtils;

/**
 * Created by v.sc.d on 2017/4/12.
 */
@Service
@Transactional
public class ShopProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductLimitDao productLimitDao;
    
    @Autowired
	private ProductPriceDao productPriceDao;

    public ShopProduct getProductDetailById(int productId){
        ShopProduct shopProduct = productDao.getProductShowTransById(productId);
        if (shopProduct == null) {
			return null;
		}
        
        shopProduct.setPriceList(productPriceDao.list(productId));
        shopProduct.setShopProductLimit(productLimitDao.getShopProductByProductId(productId));
        return  shopProduct;
    }
    
    public ShopProduct getProductShowTransById(int productId){
        ShopProduct shopProduct = productDao.getProductShowTransById(productId);
        if (shopProduct == null) {
			return null;
		}
        
        shopProduct.setShopProductLimit(productLimitDao.getShopProductByProductId(productId));
        return  shopProduct;
    }

    public boolean insertShopProduct(ShopProduct shopProduct){
        productDao.insertShopProduct(shopProduct);
        List<ShopProductLimit> shopProductLimits = shopProduct.getShopProductLimit();
        for(ShopProductLimit shopProductLimit : shopProductLimits){
            productLimitDao.insertShopProductLimit(shopProductLimit);
        }
        return true;
    }

    public boolean updateShopProduct(ShopProduct shopProduct){
        if(shopProduct.getId() == 0){
            return false;
        }
        productDao.UpdateShopProduct(shopProduct);
        for(ShopProductLimit shopProductLimit : shopProduct.getShopProductLimit()){
            productLimitDao.UpdateShopProductLimit(shopProductLimit);

        }
        return true;
    }
    
    private long lastCacheTime = 0;

	private Map<Integer, ProductSimpleInfo> cacheMap = new HashMap<>();

	public ProductSimpleInfo getProductName(int id) {
		long nowTime = System.currentTimeMillis();
		if (((lastCacheTime + CommonUtils.MAX_CACHE_TIME_30_MINUTE) > nowTime) && (!CollectionUtils.isEmpty(cacheMap))) {
			if (cacheMap.containsKey(id)) {
				try {
					return cacheMap.get(id);
				} catch (Exception e) {
				}

				return null;
			}
		}

		cacheMap = productDao.getIdNameMap();
		if (CollectionUtils.isEmpty(cacheMap)) {
			return null;
		}

		lastCacheTime = nowTime;
		if (cacheMap.containsKey(id)) {
			try {
				return cacheMap.get(id);
			} catch (Exception e) {
			}
		}

		return null;
	}
    
}